import 'package:aichat_flutter_wallet/kai_print.dart';
import 'package:aichat_flutter_wallet/module_common/widgets/dialog/easyloading.dart';
import 'package:flutter/material.dart';
import 'package:lib_database/lib_database.dart';

import '../../../module_common/utils/util.dart';

class NodeProvider extends ChangeNotifier {
  // //现在没有这个需求了，所以没有调用
  // NodeInfo insertNode(
  //     {required String chain,
  //     required String url,
  //     String? name,
  //     bool isSelected = false,
  //     int? insertIndex}) {
  //   if (isSelected) {
  //     //如果这个节点是默认选中的，那就处理之前选中的节点
  //     var selectedNodeInfo =
  //         ObjectBox().nodeInfoModel.getSelectedNodeInfo(chain);
  //     selectedNodeInfo.isSelected = false;
  //     ObjectBox().nodeInfoModel.updateNodeInfo(selectedNodeInfo);
  //   }

  //   //添加节点
  //   var info = ObjectBox().nodeInfoModel.addNodeInfo(
  //       chain: chain, url: url, name: name, isSelected: isSelected);

  //   //插入到指定位置的话就全部重新更新
  //   if (insertIndex != null) {
  //     //拿到原始
  //     var list = ObjectBox().nodeInfoModel.getNodeInfoList(chain);
  //     //删掉这个新加的节点
  //     list.removeLast();
  //     // list.remove(info);
  //     //然后插入到指定位置
  //     list.insert(insertIndex >= list.length ? list.length : insertIndex, info);
  //     //删除所有记录
  //     ObjectBox().nodeInfoModel.deleteAllNodeInfo(chain);
  //     //更新时间
  //     list.forEach((element) {
  //       element.id = 0;
  //     });
  //     //重新添加
  //     ObjectBox().nodeInfoModel.box.putMany(list);
  //   }

  //   notifyListeners();

  //   return info;
  // }
  List<NodeInfo> currentChainNodeInfoList = [];
  bool isLoading=false;

  setCurrentChainNodeInfoList({required String chain}){
    currentChainNodeInfoList =  nodeInfoList(chain:chain);
    notifyListeners();
  }



  // 更新 众多节点
  void updateNodePageList({required String chain}){

    if(isLoading){
      return;
    }
    isLoading=true;
    EasyLoading.show(status: '-1');
    List<NodeInfo> result = ObjectBox().nodeInfoModel.getNodeInfoList(chain);
    List<Future<bool>> futureList = [] ;
    for(var nodeItem in result){
      futureList.add(updateNodeBlockAndRate(nodeItem));
    }
    Future.wait(futureList).then((value){
      isLoading=false;
      EasyLoading.dismiss();
      notifyListeners();
    });
  }

  void updateNodeOne({required String chain}){
    NodeInfo result = ObjectBox().nodeInfoModel.getSelectedNodeInfo(chain);
    updateNodeBlockAndRate(result).then((value) => notifyListeners());
  }

  // 更新单个 节点信息
  Future<bool> updateNodeBlockAndRate(NodeInfo nodeInfo) async{
    try{
      final chainName= nodeInfo.chain;
      (String,String) result= await NodeRateUtils.getBlockHeightAndRate(nodeInfo.url,chainName);
      nodeInfo.rate = result.$1;
      nodeInfo.blockHeight = result.$2;
      ObjectBox().nodeInfoModel.updateNodeInfo(nodeInfo);
      return true;
    }catch(e){
      return false;
    }
  }

  Future<void> addNode(
      {required String chain,
      required String url,
      String? name,
      bool isSelected = false}) async {
    if (isSelected) {
      //如果这个节点是默认选中的，那就处理之前选中的节点
      var selectedNodeInfo =
          ObjectBox().nodeInfoModel.getSelectedNodeInfo(chain);
      selectedNodeInfo.isSelected = false;
      ObjectBox().nodeInfoModel.updateNodeInfo(selectedNodeInfo);
    }
    //添加节点
    await ObjectBox().nodeInfoModel.addNodeInfo(
        chain: chain, url: url, name: name, isSelected: isSelected);

    notifyListeners();
  }

  List<NodeInfo> nodeInfoList({required String chain}) {
    return ObjectBox().nodeInfoModel.getNodeInfoList(chain);
  }

  NodeInfo getSelectedNodeInfo({required String chain}) {
    return ObjectBox().nodeInfoModel.getSelectedNodeInfo(chain);
  }


  bool? selectNode({required String chain, required NodeInfo selectedNode}) {
    try{
      if(isLoading){
        return false;
      }
      //需要把之前选中的去掉
      var oldNode = ObjectBox().nodeInfoModel.getNodeInfoList(chain);
      List<Future<int>> futureList = [] ;
      for(var item in oldNode ){
        item.isSelected = false;
        if(selectedNode.id == item.id){
          item.isSelected = true;
        }
        futureList.add(ObjectBox().nodeInfoModel.updateNodeInfoAsync(item));
      }
      Future.wait(futureList).then((value) => notifyListeners());
      return true;
    }catch(e){
      print('selectNode $e');
      return null;
    }
  }

  void deleteNode(NodeInfo nodeInfo) {
    ObjectBox().nodeInfoModel.deleteNodeInfo(nodeInfo);

    notifyListeners();
  }
}
